import { emitter } from '@kit.BasicServicesKit'

export class Emitter<T>{
   id:number

  constructor(id) {
     this.id=id
  }

  // 订阅
  on(call:(msg:T)=>void){
    let event: emitter.InnerEvent = {
      eventId: this.id
    };
    emitter.on(event,(msg:emitter.EventData)=>{
       call(msg.data as T)
    })
  }
  // 发布
  emit(data){
    let event:emitter.InnerEvent = {
      eventId: this.id,
      priority: emitter.EventPriority.LOW
    };
    let eventData: emitter.EventData = {
      data
    };
     emitter.emit(event,eventData)
  }
}